package cn.gd.service.systemAdministration.organizations.impl;

import cn.gd.mapper.systemAdministration.Organizations.OrganizationsMapper;
import cn.gd.pojo.Department;
import cn.gd.pojo.Organizationstructure;
import cn.gd.service.systemAdministration.organizations.OrganizationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrganizationsServiceImpl implements OrganizationsService {

    @Autowired
    private OrganizationsMapper mapper;

    public List<Organizationstructure> getAllOrganizations() {
        return mapper.getAllOrganizations();
    }

    @Override
    public Department getDepartmentById(Integer id) {
        return mapper.getDepartmentById(id);
    }

    @Override
    public Organizationstructure getOrganizationStructureNameById(Integer deptId, Integer orgId) {
        return mapper.getOrganizationStructureNameById(deptId, orgId);
    }

    @Override
    public Boolean updateDepartmentById(Department department) {
        int row = mapper.updateDepartmentById(department);
        boolean temp = false;
        if(row > 0){
            temp = true;
        }
        return temp;
    }

    @Override
    public Boolean deleteDepartmentById(Integer id) {
        int row = mapper.deleteDepartmentHierarchyByDeptID(id);
        boolean temp = false;
        if(row > 0){
            int depRow = mapper.deleteDepartmentById(id);
            if(depRow > 0){
                temp = true;
            }
        }
        return temp;
    }

    @Override
    public Organizationstructure getOrganizationstructureByOrgId(Integer orgId) {
        return mapper.getOrganizationstructureByOrgId(orgId);
    }

    @Override
    public Boolean updateOrganizationalByOrgId(Organizationstructure organizationstructure) {
        int orgId = mapper.queryOrganizationStructureBySortOrder(organizationstructure.getSortOrder());
        boolean temp = false;
        int row;
        if(orgId == organizationstructure.getOrgID()){
            row = mapper.updateOrganizationStructureByOrgId(organizationstructure);
        }else{
            mapper.AllOfSortOrderPlusOneBySortOrder(organizationstructure.getSortOrder());
            row = mapper.updateOrganizationStructureByOrgId(organizationstructure);
        }
        if(row > 0){
            temp = true;
        }
        return temp;
    }

    @Override
    public Boolean addDepartment(Department department,Integer orgId) {
        int row = mapper.addDepartment(department);
        boolean temp = false;
        if(row > 0){
            int row1 = mapper.addDepartmentHierarchy(orgId,department.getDeptID());
            if(row1 > 0){
                temp = true;
            }
        }
        return temp;
    }

    @Override
    public Boolean deleteOrganizationStructureByOrgId(Integer orgId) {
        boolean temp = false;
        int sortOrder = mapper.querySortOrderByOrgId(orgId);
        mapper.deleteDepartmentHierarchyByOrgId(orgId);
        mapper.AllOfSortOrderMinusOneBySortOrder(sortOrder);
        int row2 = mapper.deleteOrganizationStructureByOrgId(orgId);//删除组织结构
        if(row2 > 0){
            temp = true;
        }
        return temp;
    }

    @Override
    public Boolean addOrganizationStructure(Organizationstructure organizationstructure) {
        int orgId = mapper.queryOrganizationStructureBySortOrder(organizationstructure.getSortOrder());
        boolean temp = false;
        int row;
        if(orgId != 0){
            mapper.AllOfSortOrderPlusOneBySortOrder(organizationstructure.getSortOrder());
        }
        row = mapper.addOrganizationStructure(organizationstructure);
        if(row > 0){
            temp = true;
        }
        return temp;
    }
}
